package com.binghe.protocol.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

import com.binghe.constant.Encode;
import com.binghe.params.Request;
import com.binghe.params.Response;
import com.binghe.utils.ProtocolUtils;

/**
 * 客户端代码
 * @author binghe
 *
 */
public final class Client {
    public static void main(String[] args) throws IOException{
        //请求
        Request request = new Request();
        Scanner sc=new Scanner(System.in);
        String ch=sc.nextLine();
        request.setCommand(ch);
        request.setCommandLength(request.getCommand().length());
        request.setEncode(Encode.UTF8);

        Socket client = new Socket("127.0.0.1", 4567);
        OutputStream out = client.getOutputStream();

        //发送请求
        ProtocolUtils.writeRequest(out, request);

        //读取响应数据
        InputStream in = client.getInputStream();
        Response response = ProtocolUtils.readResponse(in);
        System.out.println("获取的响应结果信息为: " + response.toString());
    }
}
